package com.test.io.raderWriter;

import org.junit.Test;

import java.io.FileWriter;
import java.io.IOException;

// 字符输出流练习
public class FileWriterDemo {
    // 使用字符输出流输出数据到指定文件
    @Test
    public void writeFile() {
        FileWriter fileWriter = null;
        try {
            // new FileWriter(path, true)  ture 为追加写入
            fileWriter = new FileWriter("d:\\writeFile.text", true);
            char[] chars = {'a', 'b', 'c'};
            // 写入单个字符串
            fileWriter.write("字符输出流");
            // 写入指定数组
            fileWriter.write(chars);
            // 写指定数组的指定部分
            fileWriter.write("山重水复疑无路".toCharArray(), 0, 3);
            // 写入字符串
            fileWriter.write(" hello");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(fileWriter != null) {
                // flush/close 会执行 out.writer 方法想文件写入字符,
                try {
                    fileWriter.flush();
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                }
            }
        }
    }
}
